package com.hxzy.bbs.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;

import com.hxzy.jsp.connection.JNDIBBSConection;
import com.hxzy.jsp.session.ErrorCode;
import com.hxzy.jsp.utils.Connections;
import com.hxzy.jsp.utils.Hashs;
import com.mysql.jdbc.StringUtils;

public class RegisterServlet extends AppServlet {

	private static final long serialVersionUID = 1L;
	
	public RegisterServlet() {
		super();
	}

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String passwd = request.getParameter("passwd");
		String nickname = request.getParameter("nickname");
		String email = request.getParameter("email");

		if (StringUtils.isEmptyOrWhitespaceOnly(username)) {
			response.sendRedirect("./error?error=" + ErrorCode.FIELD_REQUIRE);
			return;
		}
		if (StringUtils.isEmptyOrWhitespaceOnly(passwd)) {
			response.sendRedirect("./error?error=" + ErrorCode.FIELD_REQUIRE);
			return;
		}
		if (StringUtils.isEmptyOrWhitespaceOnly(nickname)) {
			response.sendRedirect("./error?error=" + ErrorCode.FIELD_REQUIRE);
			return;
		}

		Calendar c = Calendar.getInstance();

		Connection conn = null;
		try {
			conn = new JNDIBBSConection().get();
			PreparedStatement stmt = null;
			// 检查用户名是否重复
			stmt = conn.prepareStatement("select 1 from `stu_bbs`.`user` where username = ?");
			stmt.setString(1, username);
			ResultSet rs = stmt.executeQuery();
			boolean isUsernameExists = false;
				while (rs.next()) {
					isUsernameExists = true;
					break;
				}
			if (isUsernameExists) {
				response.sendRedirect("./error?error=" + ErrorCode.USER_REGISTE_DUPLICATE);
				return;
			}
				long now = (int) (c.getTimeInMillis() / 1000);
				stmt = conn.prepareStatement("insert into `stu_bbs`.`user` "
						+ "(`username`, `passwd`, `salt`, `nickname`, `email`, `gmt_create`, `ip_reg`, `status`) "
						+ "values (?, ?, ?, ?, ?, ?, ?, 0)");
				String salt = Hashs.createSalt();
				stmt.setString(1, username);
				stmt.setString(2, DigestUtils.sha1Hex((salt + passwd).getBytes()));
				stmt.setString(3, salt); // 散列值，64 位

				stmt.setString(4, nickname);
				stmt.setString(5, email);
				stmt.setLong(6, now);
				stmt.setString(7, request.getRemoteAddr());

				stmt.execute();
				stmt.close();
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		response.sendRedirect("./login");
}

}
